package com.kg.rails.ui.panels.property;

import com.kg.rails.component.editor.model.Cell;
import com.kg.rails.component.editor.model.custom.ElementCell;
import com.kg.rails.component.editor.model.custom.NodeCell;
import com.kg.rails.component.editor.model.custom.StationCell;
import com.kg.rails.component.editor.swing.GraphComponent;
import com.kg.rails.ui.util.UIHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * User: Babanin
 * Date: 05.05.12
 * Time: 1:23
 */
public abstract class PropertyPanel<T extends Cell> extends JPanel {
    protected T cell;
    private Map<String, JComponent> componentMap;

    private JButton saveButton;
    private JButton resetButton;

    protected JTextField nameTextField;
    protected GraphComponent component;

    public PropertyPanel(T cell, GraphComponent component) {
        this.cell = cell;
        this.component = component;
        this.nameTextField = new JTextField();

        this.componentMap = new LinkedHashMap<String, JComponent>();

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        saveButton = new JButton(new AbstractAction("Сохранить") {
            public void actionPerformed(ActionEvent e) {
                save();
            }
        });

        resetButton = new JButton(new AbstractAction("Сбросить") {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });

        addProperty("Название: ", nameTextField);
    }

    protected void save() {
        cell.setValue(nameTextField.getText().trim());

        component.refresh();
    }

    protected void reset() {
        nameTextField.setText(cell.getValue().toString());
    }

    public void addProperty(String description, JComponent component) {
        componentMap.put(description, component);
    }

    public JPanel build() {
        removeAll();
        setLayout(new BorderLayout());

        int i = 0;
        Object[] components = new Object[componentMap.size() * 2];
        for (Map.Entry<String, JComponent> entry : componentMap.entrySet()) {
            components[i] = new JLabel(entry.getKey());
            components[i + 1] = entry.getValue();

            i += 2;
        }

        add(UIHelper.createGridPanel(5, 5, 5, 5, componentMap.size(), 2, components), BorderLayout.NORTH);
        add(UIHelper.packInPanel(UIHelper.hBox(saveButton, 2, resetButton), BorderLayout.LINE_END), BorderLayout.SOUTH);

        reset();

        return this;
    }

    public T getCell() {
        return cell;
    }

    public static PropertyPanel getSuitablePanel(Cell cell, GraphComponent component) {
        if (cell instanceof StationCell) {
            return new StationPropertyPanel((StationCell) cell, component);
        } else if (cell instanceof ElementCell) {
            return new ElementPropertyPanel((ElementCell) cell, component);
        } else if (cell instanceof NodeCell) {
            return new NodePropertyPanel((NodeCell) cell, component);
        }

        return null;
    }

    public abstract String getDescription();
}
